package com.example.zhinongbao.activity;
// 隐私设置


import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Switch;

import com.example.zhinongbao.R;

public class PrivacySettingsActivity extends AppCompatActivity {

    private static final String PREFS_NAME = "PrivacyPrefs";
    private static final String KEY_PERSONALIZED = "personalized";
    private static final String KEY_NOTIFICATIONS = "notifications";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_settings);

        // 初始化Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        ImageButton backButton = findViewById(R.id.backButton);
        backButton.setOnClickListener(v -> finish());

        // 初始化开关
        Switch switchPersonalized = findViewById(R.id.switchPersonalized);
        Switch switchNotifications = findViewById(R.id.switchNotifications);

        // 加载保存的设置
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        switchPersonalized.setChecked(prefs.getBoolean(KEY_PERSONALIZED, true));
        switchNotifications.setChecked(prefs.getBoolean(KEY_NOTIFICATIONS, true));

        // 设置开关监听
        switchPersonalized.setOnCheckedChangeListener((buttonView, isChecked) ->
                savePreference(KEY_PERSONALIZED, isChecked));

        switchNotifications.setOnCheckedChangeListener((buttonView, isChecked) ->
                savePreference(KEY_NOTIFICATIONS, isChecked));
    }

    private void savePreference(String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
        editor.putBoolean(key, value);
        editor.apply();

        // 这里可以添加实际功能代码，例如：
        if (key.equals(KEY_PERSONALIZED)) {
            // 更新个性化推荐服务状态
        } else if (key.equals(KEY_NOTIFICATIONS)) {
            // 更新推送通知服务状态
        }
    }
}
